﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;


namespace YouHP.Pages.User
{
    public class ShowPm : BasePage
    {

        protected int msgid;
        protected int parentid;
        protected MessageInfo MsgData = new MessageInfo();
        protected List<MessageInfo> MsgReplyList = new List<MessageInfo>();

        protected int pageIndex = 1;
        protected int pageSize = 15;
        protected int pageCount = 0;
        protected int dataCount = 0;

        protected string PagingString = "";
       


        protected override void ShowPage()
        {
            base.ShowPage();
            this.AddLocation("查看短消息", "");
            this.SetTitle("查看短消息");
            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();


           
        }

        private void HandleCommon()
        {

            msgid = RequestTools.GetQueryInt("id", -1);
            parentid = RequestTools.GetQueryInt("parentid", -1);
            if (parentid == 0)
            {
                MsgData = MessageBLL.Instance.GetMessage(msgid);
            }
            else
            {
                MsgData = MessageBLL.Instance.GetMessage(parentid);
            }

        }

        private void HandlePost()
        {

            MessageInfo NewMsg = new MessageInfo();
            if (MsgData.msgfromid == UserData.Id)
            {
                NewMsg.msgtoid = MsgData.msgtoid;
            }
            else
            {
                NewMsg.msgtoid = MsgData.msgfromid;
            }

            NewMsg.msgto = UserBLL.Instance.GetUserInfo(NewMsg.msgtoid).UserName;
            NewMsg.msgfromid = UserData.Id;
            NewMsg.msgfrom = UserData.UserName;
            NewMsg.PostDateTime = DateTime.Now;
            NewMsg.message = RequestTools.GetString("reply").Trim();
            NewMsg.ParentId = MsgData.Id;
            NewMsg.IsRead = 0;

            bool succ = MessageBLL.Instance.CreateMessage(NewMsg);
            if (succ)
            {
                this.Redirect("/User/ShowPm.aspx?id="+MsgData.Id+"&parentid="+MsgData.ParentId);

            }

            
        }

        private void HandleGet()
        {

            pageIndex = RequestTools.GetQueryInt("page", 1);
            //回复
            MsgReplyList = MessageBLL.Instance.GetMsgReplyList(pageSize, pageIndex, MsgData.Id, out dataCount);

            pageCount = Utils.GetPageCount(dataCount, pageSize);

            string query = System.Web.HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PagingString = this.GetPagingString(
                pageIndex,
                pageCount,
                "/User/ShowPm.aspx" + query, "page");
        }

    }
}
